package com.github.freegeese.maven.plugin.autocode.demo.base;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 基础的控制层，集成增删改查基础接口
 *
 * @param <S>  业务Service
 * @param <M>  持久化Mapper
 * @param <E>  实体Entity
 * @param <ID> 实体主键ID
 */
public abstract class BaseController<S extends BaseService<M, E, ID>, M extends BaseMapper<E, ID>, E extends BaseEntity<ID>, ID> {
    @Autowired
    S service;

    /**
     * 读取所有资源
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public Object readAll() {
        return service.selectAll();
    }

    /**
     * 读取资源
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object read(@PathVariable("id") ID id) {
        return service.selectByPrimaryKey(id);
    }

    /**
     * 读取资源列表
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/list/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object readList(@PathVariable("ids") ID[] ids) {
        return service.selectByPrimaryKeys(Arrays.asList(ids));
    }

    /**
     * 分页读取资源
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/page/{pageNumber}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public Object readPage(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize) {
        return service.selectPage(new PageImpl(pageNumber, pageSize));
    }

    /**
     * 创建资源
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestBody E entity) {
        return service.insertSelective(entity);
    }

    /**
     * 创建资源列表
     *
     * @param entities
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object createList(@RequestBody List<E> entities) {
        return service.batchInsert(entities);
    }

    /**
     * 修改资源
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public Object update(@RequestBody E entity) {
        return service.updateByPrimaryKeySelective(entity);
    }

    /**
     * 修改资源列表
     *
     * @param entities
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.PUT)
    @ResponseBody
    public Object updateList(@RequestBody List<E> entities) {
        return service.batchUpdate(entities);
    }

    /**
     * 删除资源
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Object delete(@PathVariable("id") ID id) {
        return service.deleteByPrimaryKey(id);
    }

    /**
     * 删除资源列表
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/list/{ids}", method = RequestMethod.DELETE)
    @ResponseBody
    public Object deleteList(@PathVariable("ids") ID[] ids) {
        return service.deleteByPrimaryKeys(Arrays.asList(ids));
    }

    public S getService() {
        return service;
    }
}
